package org.dromara.xhlj.crud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import org.dromara.common.satoken.utils.LoginHelper;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xhlj.domain.vo.XhljGoodsVo;
import org.dromara.xhlj.domain.query.XhljGoodsQuery;
import org.dromara.xhlj.domain.bo.XhljGoodsBo;
import org.dromara.xhlj.crud.service.IXhljGoodsCrudService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * xhlj-crud-商品
 *
 * @author yf
 * @date 2024-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xhlj/goods")
public class XhljGoodsCrudController extends BaseController {

    private final IXhljGoodsCrudService xhljGoodsService;

    /**
     * 分页查询商品列表
     */
    @SaCheckPermission("xhlj:goods:list")
    @GetMapping("/list")
    public TableDataInfo<XhljGoodsVo> list(XhljGoodsQuery query, PageQuery pageQuery) {
         return xhljGoodsService.queryPageList(query, pageQuery);
    }

    /**
     * 查询商品列表
     */
    @SaCheckPermission("xhlj:goods:list")
    @GetMapping("/allList")
    public TableDataInfo<XhljGoodsVo> allList(XhljGoodsQuery query) {
        List<XhljGoodsVo> list =  xhljGoodsService.queryList(query);
        return TableDataInfo.build(list);
    }


    /**
     * 导出商品列表
     */
    @SaCheckPermission("xhlj:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XhljGoodsQuery query, HttpServletResponse response) {
        List<XhljGoodsVo> list = xhljGoodsService.queryList(query);
        ExcelUtil.exportExcel(list, "商品", XhljGoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param goodId 主键
     */
    @SaCheckPermission("xhlj:goods:query")
    @GetMapping("/{goodId}")
    public R<XhljGoodsVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer goodId) {
        return R.ok(xhljGoodsService.queryById(goodId));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("xhlj:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XhljGoodsBo bo) {
        bo.setCreateBy(Math.toIntExact(LoginHelper.getUserId()));
        return toAjax(xhljGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("xhlj:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XhljGoodsBo bo) {
        return toAjax(xhljGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param goodIds 主键串
     */
    @SaCheckPermission("xhlj:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] goodIds) {
        return toAjax(xhljGoodsService.deleteWithValidByIds(List.of(goodIds), true));
    }
}
